package chapter1.thread.task;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

/*
附件《lottery2.txt》是最近几年中国福利彩票的中奖号码，
存储格式是 期号:红球号码用空格隔开，后面是+篮球号，统计出现次数最多的红球号码和蓝球号码
 */

/*
-1:
上次的那个彩票案例，按照从大到小输出红球出现的次数；
按照从大到小输出蓝球出现的次数；
 */
public class Task1 {
    public static void main(String[] args) throws Exception {
        HashMap<String, Integer> map = new HashMap<>();
        HashMap<String, Integer> map2 = new HashMap<>();
        BufferedReader in = new BufferedReader(new FileReader("F:\\123\\lottery2.txt"));
        String s = "";
        String red = "", blue = ""; //红，蓝球号码
        int a = 0, b = 0;
        int countRed = 1, countBlue = 1; //假设红球和篮球都只有一个重复
        while ((s = in.readLine()) != null) {
            a = s.indexOf(':');
            b = s.indexOf('+');
            red = s.substring(a + 1, b);

            String[] str = red.split("\\s");  //空格
            for (int i = 0; i < str.length; i++) {
                if (map.containsKey(str[i]))//红球
                    map.put(str[i], map.get(str[i]) + 1);
                else
                    map.put(str[i], 1);
            }


            blue = s.substring(b + 1);
            if (map2.containsKey(blue)) //篮球
                map2.put(blue, map2.get(blue) + 1);
            else
                map2.put(blue, 1);
        }

        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> c : entries) {
            if (c.getValue() > countRed) {
                countRed = c.getValue();
                red = c.getKey();
            }
        }
        if (countRed > 1)
            System.out.println("红球号码重复最多的为：" + red + "，重复次数：" + countRed);


        Set<Map.Entry<String, Integer>> entries2 = map2.entrySet();
        for (Map.Entry<String, Integer> c : entries2) {
            if (c.getValue() > countBlue) {
                countBlue = c.getValue();
                blue = c.getKey();
            }
        }
        if (countBlue > 1)
            System.out.println("篮球号码重复最多的为：" + blue + "，重复次数：" + countBlue);
        System.out.println();


        System.out.println("红球的重复次数从大到小输出：");
        int count1 = 0;
        while (map.size() > 0) {
            int value = Collections.max(map.values());
            Set<Map.Entry<String, Integer>> en = map.entrySet();
            Iterator<Map.Entry<String, Integer>> iter = en.iterator();
            while (iter.hasNext()) {
                Map.Entry<String, Integer> c = iter.next();
                if (c.getValue() == value) {
                    System.out.print(c.getKey() + "，" + value + "次" + "\t");
                    iter.remove();
                    count1++;
                    if (count1 % 10 == 0)
                        System.out.println();
                }
            }
        }
        System.out.println("\n");


        System.out.println("篮球的重复次数从大到小输出：");
        int count2 = 0;
        while (map2.size() > 0) {
            int value = Collections.max(map2.values());
            Set<Map.Entry<String, Integer>> en = map2.entrySet();
            Iterator<Map.Entry<String, Integer>> iter = en.iterator();
            while (iter.hasNext()) {
                Map.Entry<String, Integer> c = iter.next();
                if (c.getValue() == value) {
                    System.out.print(c.getKey() + "，" + value + "次" + "\t");
                    iter.remove();
                    count2++;
                    if (count2 % 10 == 0)
                        System.out.println();
                }
            }
        }


    }
}
